#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

#define THREAD_STACK_SIZE (1024 * 4)
#define THREAD_PRIO 25

uint32_t secs1, secs2;
osThreadId_t tid1, tid2;

void thread1_entry()
{
    while (1)
    {
        // 计数器加1
        secs1++;
        printf("task1 demo\n");
        if (secs1 % 3 == 0)
        {
            printf("==========\n");
            // 恢复任务2
            osThreadResume(tid2);
            // 挂起任务1
            osThreadSuspend(tid1);
        }
        // 睡眠100tick，即1s
        osDelay(100);
    }
    return 0;
}

void thread2_entry()
{
    while (1)
    {
        // 计数器加1
        secs2++;
        printf("task2 demo\n");
        if (secs2 % 3 == 0)
        {
            printf("==========\n");
            // 恢复任务1
            osThreadResume(tid1);
            // 挂起任务2
            osThreadSuspend(tid2);
        }
        // 睡眠100tick，即1s
        osDelay(100);
    }
    return 0;
}
void demo_task(VOID)
{
    osThreadAttr_t attr;

    // 线程名字
    attr.name = "Thread1";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    // 线程栈大小
    attr.stack_size = THREAD_STACK_SIZE;
    // 线程优先级
    attr.priority = THREAD_PRIO;

    // 创建任务1
    tid1 = osThreadNew((osThreadFunc_t)thread1_entry, NULL, &attr);
    if (tid1 == NULL) {
        printf("Failed to create Thread1!\n");
    }

    // 创建任务2
    attr.name = "Thread2";
    tid1 = osThreadNew((osThreadFunc_t)thread2_entry, NULL, &attr);
    if ( tid2 == NULL) {
        printf("Failed to create Thread2!\n");
    }
}
APP_FEATURE_INIT(demo_task);